package com.jwaoo.mobile.http;

import com.dx.as.common.JWResult;
import com.dx.as.common.string.MD5;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jwaoo.mobile.view.account.AccountBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import http.HttpClientHelper;
import http.IHttpListener;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Created by jwaoo on 2017/7/14.
 */

public class HttpHelper {
    private final static Gson gson = new Gson();
    private static IHttpServices http;
    private static IHttpServices getHttpSevices(){
        if (http == null){
            http = HttpClientHelper.getRetrofit().create(IHttpServices.class);
        }
        return http;
    }

    public static void doLogin(Map<String,Object> param,IHttpListener listener){
        String jsonStr = gson.toJson(param);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonStr);
        Call call = getHttpSevices().postLogin(IHttpServices.login,body);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<Map<String,String>>>(){});
    }

    public static void getAccountInfo(IHttpListener listener){
        Call call = getHttpSevices().getAccountInfo(IHttpServices.account);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<AccountBean>>(){});
    }

    public static void getAccountInfoList(IHttpListener listener){
        Call call = getHttpSevices().getAccountInfoList(IHttpServices.users);
        HttpClientHelper.doNet(call,listener,new TypeToken<JWResult<List<AccountBean>>>(){});
    }
}
